package eventReservationServer.saxParser;

import java.util.Collection;

import org.xml.sax.Attributes;
import org.xml.sax.SAXParseException;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import util.Technology;

public class EventServerGatewaysSAXHandler extends DefaultHandler
{
	private ServicesList list = new ServicesList();
	private Service service;
	private String texto;

	// ============================================================================
	// Métodos de DefaultHandler y pertenecimientes a ContentHandler
	// sobreescritos
	// ============================================================================
	public void startDocument() throws SAXException
	{
		System.out.println("# Comenzando el parseo del documento...");
		this.service = new Service();
		this.texto = "";
	}

	public void endDocument() throws SAXException
	{
		System.out.println("# Parseo del documento finalizado.");
	}

	public void startElement(String namespaceURI, String lName, // local name
			String qName, Attributes attrs) throws SAXException
	{
		System.out.println(" * [Etiqueta] -> <" + qName + ">");
		if (qName.equals("server"))
		{
			service = new Service();
			list.add(service);
			this.service.setName(attrs.getValue("name"));
			System.out.println(" - [Atributo] -> name='"
					+ attrs.getValue("name") + "'");
		}
	}

	public void endElement(String namespaceURI, String lName, String qName)
			throws SAXException
	{
		if (this.texto.length() > 0)
		{
			System.out.println(" - [Texto] -> " + texto);
			if (qName.equals("technology"))
			{
				if ( this.texto.equals("corba") )
					this.service.setTech(Technology.corba);
				else if ( this.texto.equals("jms") )
					this.service.setTech(Technology.jms);
				else if ( this.texto.equals("ws") )
					this.service.setTech(Technology.ws);
				else
					this.service.setTech(null);
			}
			else if (qName.equals("ip"))
			{
				this.service.setIp(this.texto);
			}
			else if (qName.equals("port"))
			{
				this.service.setPort(this.texto);
			}
		}
		this.texto = "";
		System.out.println(" * [Etiqueta] -> </" + qName + ">");
	}

	public void characters(char[] ch, int start, int length)
			throws SAXException
	{
		String s = String.valueOf(ch, start, length).trim();
		this.texto += s;
	}

	public void processingInstruction(String target, String data)
			throws SAXException
	{
		System.out.println(" * [PI] -> <?" + target + data + "?>");
	}

	// ============================================================================
	// Métodos de DefaultHandler y pertenecimientes a ErrorHandler sobreescritos
	// ============================================================================
	public void error(SAXParseException exception) throws SAXException
	{
		System.out.println("# [ERROR] -> " + exception.getMessage());
	}

	public void warning(SAXParseException exception) throws SAXException
	{
		System.out.println("# [WARNING] -> " + exception.getMessage());
	}

	public void fatalError(SAXParseException exception) throws SAXException
	{
		System.out.println("# [FATAL ERROR] -> " + exception.getMessage());
	}

	// ================
	// Métodos propios
	// ================
	@Override
	public String toString()
	{
		return super.toString(); //TODO
	}
	
	public Collection<Service> getServices()
	{
		return list.getList();
	}
}